clear all
set more off, perm
set mem 10000000
set matsize 10000
version 12


*************************************************************** 
*** Assign distance to opposite plan, at village level  *******
*************************************************************** 

** Set file paths
do "$path_code/paths.do"

********************************************************************************
********************************************************************************

** Create village dataset with village distances to opposite plan boundaries
{
  // drop old .dta files		
cd "$path/data/village gis maps/points_with_dd_dists"  
local files : dir . files "*distances.dta"
foreach file in `files' {
  erase `file'
}

  // convert output from python script into .dta files
local files : dir . files "*.txt"
foreach file in `files' {
	insheet using `file', comma clear
	local state = subinstr(subinstr(state_ut," ","_",.),"&","and",.)
	keep c_code01 tenthplandist eleventhplandist
	gen state = "`state'"
	tostring c_code01, replace format(%30.0f)
	cap confirm file "`state'_distances.dta"
	  if _rc == 0 {
	    save "`state'_2_distances.dta", replace 
	  }
	  else {  
        save "`state'_distances.dta", replace
	  }
}

  // fix the string issues for Rajasthan, Uttranchal, and Punjab
foreach state in Rajasthan Uttranchal Punjab {
	use "`state'_distances.dta", clear
	replace c_code01 = "0" + c_code01
	save "`state'_distances.dta", replace
}

  // append .dta files from all states
clear
local stata : dir . files "*.dta"
foreach statafile in `stata' {
	di "`statafile'"
	append using "`statafile'"
}

  // clean up village codes and collpase to village level
drop if length(c_code01)<16
gen st_code = substr(c_code01,1,2)
gen dt_code = substr(c_code01,3,2)
gen vi_code = substr(c_code01,9,8)
destring st_code dt_code vi_code, replace force
drop if st_code > 35 | vi_code==.
collapse (max) tenthplandist eleventhplandist, by(st_code dt_code vi_code) 
duplicates drop

  // correct the few cases where distance and plan do not match!  
merge m:m st_code dt_code using "$rggvy/rggvy_district_progress_X_XI_processed.dta", nogen keep(1 3) keepusing(vdpr4 vplan4)
replace tenthplandist = 0 if vplan4==10
replace eleventhplandist = 0 if vplan4==11
replace tenthplandist = 0 if vplan4>10 & vplan4<11
replace eleventhplandist = 0 if vplan4>10 & vplan4<11 
  
  // define distance to opposite plan, label and save
egen distance_to_opp_plan = rowmax(tenthplandist eleventhplandist)
la var tenthplandist "Distance to 10th Plan districts (km)"
la var eleventhplandist "Distance to 11th Plan districts (km)"
la var distance_to_opp_plan "Distance to opposite plan (km)"
la var st_code "2001 state code"
la var dt_code "2001 district code"
la var vi_code "2001 village code"
compress
save "$path/data/village gis maps/villages_with_distances_all.dta", replace

}


